package com.education.service.vod.controller.web;

import com.aliyuncs.exceptions.ClientException;
import com.education.exception.OnlineEducationException;
import com.education.result.R;
import com.education.result.ResultCodeEnum;
import com.education.service.vod.service.VideoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.exception.ExceptionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.*;

/**
 * 用户访问课程播放
 * Created by Lance on 2020/07/02 10:45
 */
@Api(tags = "阿里云视频点播")
@RestController
@RequestMapping("/web/vod/media")
@Slf4j
public class WebMediaController {

    @Autowired
    private VideoService videoService;

    /**
     * 获取播放凭证
     * @param videoSourceId
     * @return
     */
    @ApiOperation("根据视频ID获取播放凭证")
    @GetMapping("get-play-auth/{videoSourceId}")
    public R getPlayAuth(@ApiParam(value = "阿里云视频ID", required = true) @PathVariable String videoSourceId){
        String  playAuth = null;
        try {
            playAuth = videoService.getPlayAuth(videoSourceId);
            return R.ok().message("获取阿里云播放凭证!").data("playAuth", playAuth);
        } catch (ClientException e) {
            log.error(ExceptionUtils.getStackTrace(e));
            throw new OnlineEducationException(ResultCodeEnum.FETCH_PLAYAUTH_ERROR);
        }

    }
}
